package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author 黄子弟
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {
    @Autowired
    private CourseBaseServiceImpl courseBaseService;
    @Override
    public List<CourseTeacherDTO> queryCourseTeacherList(Long courseBaseId, Long companyId) {
        //关键数据
        if (ObjectUtils.isEmpty(companyId)||ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }

        //业务数据，课程要存在
       courseBaseService.getCourseBase(courseBaseId, companyId);
        //获取教师信息
        LambdaQueryWrapper<CourseTeacher> wrapper = Wrappers.lambdaQuery(CourseTeacher.class);
        wrapper.eq(CourseTeacher::getCourseId, courseBaseId);
        List<CourseTeacher> list = this.list(wrapper);

        List<CourseTeacherDTO> dtos = Collections.emptyList();
        if (!ObjectUtils.isEmpty(list)) {
            dtos = CourseTeacherConvert.INSTANCE.entitys2dtos(list);
        }

        return dtos;
    }

    @Override
    @Transactional
    public CourseTeacherDTO createOrModifyCourseTeacher(CourseTeacherDTO courseTeacherDTO, Long companyId) {
        //关键数据
        if (ObjectUtils.isEmpty(courseTeacherDTO.getTeacherName())) {
            ExceptionCast.cast(ContentErrorCode.E_120413);
        }

        if (ObjectUtils.isEmpty(courseTeacherDTO.getPosition())) {
            ExceptionCast.cast(ContentErrorCode.E_120007);
        }
        CourseTeacherDTO dto = null;
        if (ObjectUtils.isEmpty(courseTeacherDTO.getCourseTeacherId())) {
            dto  =  createCourseTeacher(courseTeacherDTO,companyId);
        } else {
            dto = modifyCourseTeacher(courseTeacherDTO,companyId);
        }

        return dto;


    }

    @Override
    @Transactional
    public void deleteCourseTeacher(Long courseId, Long courseTeacherId, Long companyId) {
        //关键数据
        if (ObjectUtils.isEmpty(companyId)||ObjectUtils.isEmpty(courseId)||ObjectUtils.isEmpty(courseTeacherId)) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }

        //业务数据,只能删除自己机构下绑定的自家课程的教师信息
        CourseTeacher courseTeacher = this.getById(courseTeacherId);
        if (ObjectUtils.isEmpty(courseTeacher)) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }
        //不是自己课程下的教师信息不能删
        if (!courseId.equals(courseTeacher.getCourseId())) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }

        //不是同一家机构不能删
        CourseBase courseBase = courseBaseService.getById(courseId);
        if (!companyId.equals(courseBase.getCompanyId())) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }

        //删除教师信息
        boolean remove = this.removeById(courseTeacherId);
        if (!remove) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }
    }

    private CourseTeacherDTO modifyCourseTeacher(CourseTeacherDTO courseTeacherDTO, Long companyId) {
        //关键数据
        if (ObjectUtils.isEmpty(courseTeacherDTO.getCourseTeacherId())) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }

        //业务数据
        CourseBase courseBase = courseBaseService.getCourseBase(courseTeacherDTO.getCourseId(), companyId);
        CourseTeacher courseTeacher = this.getById(courseTeacherDTO.getCourseTeacherId());
        if (ObjectUtils.isEmpty(courseTeacher)) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }

        LambdaUpdateWrapper<CourseTeacher> wrapper = Wrappers.lambdaUpdate(CourseTeacher.class);
        wrapper.set(CourseTeacher::getTeacherName, courseTeacherDTO.getTeacherName())
                .set(CourseTeacher::getIntroduction, courseTeacherDTO.getIntroduction())
                .set(CourseTeacher::getPhotograph, courseTeacherDTO.getPhotograph())
                .set(CourseTeacher::getPosition, courseTeacherDTO.getPosition())
                .eq(CourseTeacher::getId,courseTeacherDTO.getCourseTeacherId());

        boolean update = this.update(wrapper);
        if (!update) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }

        CourseTeacher po = this.getById(courseTeacherDTO.getCourseTeacherId());
        CourseTeacherDTO dto = CourseTeacherConvert.INSTANCE.po2dto(po);

        return dto;
    }

    private CourseTeacherDTO createCourseTeacher(CourseTeacherDTO courseTeacherDTO, Long companyId) {
        //业务数据，课程要存在
        CourseBase courseBase = courseBaseService.getCourseBase(courseTeacherDTO.getCourseId(), companyId);
        //新增数据
        CourseTeacher courseTeacher = new CourseTeacher();
        courseTeacher.setCourseId(courseTeacherDTO.getCourseId());
        courseTeacher.setCoursePubId(courseTeacherDTO.getCoursePubId());
        courseTeacher.setTeacherName(courseTeacherDTO.getTeacherName());
        courseTeacher.setIntroduction(courseTeacherDTO.getIntroduction());
        courseTeacher.setPhotograph(courseTeacherDTO.getPhotograph());
        courseTeacher.setPosition(courseTeacherDTO.getPosition());
        boolean save = this.save(courseTeacher);
        if (!save) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }

        CourseTeacher po = this.getById(courseTeacher.getId());
        CourseTeacherDTO dto = CourseTeacherConvert.INSTANCE.po2dto(po);
        return dto;

    }
}
